package com.luoying.itew.kt

import android.os.Parcel
import android.os.Parcelable
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import cn.jpush.android.api.JPushInterface
import com.luoying.itew.R
import com.luoying.itew.activity.BaseActivity
import com.luoying.itew.activity.MainActivity
import com.luoying.itew.activity.MainActivityViewModel
import com.luoying.itew.databinding.ActivityMainChangeBinding
import com.luoying.itew.databinding.MarkviewChartBinding
import kotlinx.android.synthetic.main.activity_main_change.*
import org.greenrobot.eventbus.EventBus

class MainActivity() : BaseActivity<ActivityMainChangeBinding>(),View.OnClickListener, Parcelable {
    /**
     * 地图类型
     */
    private val typeMap = 0
    /**
     * 列表类型
     */
   private val typeList = 1
    /**
     * 我的
     */
    private val typeMine = 2
    private lateinit var model: MainActivityViewModel
    private var currentIndex =0;

    constructor(parcel: Parcel) : this() {
        currentIndex = parcel.readInt()
    }

    override fun init() {
        super.init()
        if(!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this)
        }
        model = ViewModelProviders.of(this).get(MainActivityViewModel::class.java)
        main_fl_map.setOnClickListener{onClick(it)}
        main_fl_list.setOnClickListener{onClick(it)}
        main_fl_mine.setOnClickListener{onClick(it)}
        model.let {
            it.weather.observe(this, Observer { setWeatherInfo(it)})
            it.getWeatherInfo()
            it.userInfo.observe(this, Observer { info ->
                //获取用户信息成功开始绑定极光推送
                JPushInterface.setAlias(applicationContext, MainActivity.BINDING_SEQUENCE, info.userId.toString() + "")
            })
        }
        switchTab(typeMap)
    }

    /**
     * 改变底部菜单栏
     * @param index
     */
    private fun switchTab(index: Int) {
        if (index == currentIndex) {
            return
        }
        main_cb_map.isChecked = false
        main_cb_list.isChecked = false
        main_cb_mine.isChecked = false
        when (index){
            typeMap->{
                main_cb_map.isChecked = true
                title_tv.setText(R.string.title_map)
            }
            typeList->{
                main_cb_map.isChecked = true
                title_tv.setText(R.string.title_map)
            }
            typeMine->{
                main_cb_map.isChecked = true
                title_tv.setText(R.string.title_map)
            }
        }
//        switchFragment(index)
    }




    override fun getLayoutResId()= R.layout.activity_main_change

    override fun onClick(v: View?) {
        TODO("Not yet implemented")
    }

    private fun setWeatherInfo(weather:String){
        title_weather.text = weather
        when {
            weather.contains("雨") -> {
                //雨天
                val drawable = resources.getDrawable(R.drawable.rain)
                title_weather.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null)
            }
            weather=="多云" -> {

            }
            else -> {
                //晴天
                val drawable = resources.getDrawable(R.drawable.lightrain)
                title_weather.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null)
            }
        }
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeInt(currentIndex)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<MainActivity> {
        override fun createFromParcel(parcel: Parcel): MainActivity? {
            return null
        }

        override fun newArray(size: Int): Array<MainActivity?> {
            return arrayOfNulls(size)
        }
    }


}